package com.rightsidetech.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.system.dtos.request.FeedbackPageReq;
import com.rightsidetech.system.dtos.request.FeedbackTempSaveReq;
import com.rightsidetech.system.dtos.request.ReplyReq;
import com.rightsidetech.system.dtos.request.UpdateFeedBackStatusReq;
import com.rightsidetech.system.dtos.response.FeedbackPageResp;
import com.rightsidetech.system.dtos.response.HistoryFeedbackPageResp;
import com.rightsidetech.system.dtos.response.SysFeedbackTempResp;
import com.rightsidetech.system.dtos.response.SysFeedbackTempSearchResp;
import com.rightsidetech.system.entity.SysFeedback;
import com.rightsidetech.system.entity.SysFeedbackTemp;
import com.rightsidetech.system.mapper.SysFeedbackMapper;
import com.rightsidetech.system.mapper.SysFeedbackTempMapper;
import com.rightsidetech.system.service.SysFeedbackService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: yhr
 * @Date: 2021-12-15 11:58
 */
@Service("sysFeedbackServiceImpl")
public class SysFeedbackServiceImpl implements SysFeedbackService {
    @Resource
    private SysFeedbackTempMapper sysFeedbackTempMapper;
    @Resource
    private SysFeedbackMapper sysFeedbackMapper;

    @Override
    public ResponseData<List<SysFeedbackTempResp>> selectAllTemp() {
        List<SysFeedbackTempResp> resps = sysFeedbackTempMapper.selectAllTemp();
        return ResponseData.success(resps);
    }

    @Override
    public ResponseData saveTemp(FeedbackTempSaveReq req,Long loginId) {
        SysFeedbackTemp temp = new SysFeedbackTemp();
        temp.setQuestion(req.getQuestion());
        temp.setCreatedAt(new Date());
        temp.setCreaterId(loginId);
        sysFeedbackTempMapper.insertSelective(temp);
        return ResponseData.success();
    }

    @Override
    public ResponseData delTemp(DelReq req,Long loginInfoId) {
        SysFeedbackTemp sysFeedbackTemp = sysFeedbackTempMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(sysFeedbackTemp)){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        if (sysFeedbackTemp.getIsDeleted() == 1){
            return ResponseData.fail("不能重复删除");
        }
        //关联的不能删除
        Long count = sysFeedbackMapper.selectByTempId(req.getId());
        if (count > 0){
            return ResponseData.fail("已关联,不允许删除");
        }
        sysFeedbackTempMapper.updateDelById(req.getId(),loginInfoId);
        return ResponseData.success();
    }

    @Override
    public ResponseData<List<SysFeedbackTempSearchResp>> search() {
        return ResponseData.success(sysFeedbackTempMapper.search());
    }

    @Override
    public ResponseData<PageInfo<FeedbackPageResp>> page(FeedbackPageReq req) {
        PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<FeedbackPageResp> page = sysFeedbackMapper.page(req);
        return ResponseData.success(new PageInfo<>(page));
    }

    @Override
    public ResponseData reply(ReplyReq req,Long loginInfoId) {
        SysFeedback sysFeedback = sysFeedbackMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(sysFeedback) ){
           return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        if (2 == sysFeedback.getStatus()){
            return ResponseData.fail("此反馈已回复");
        }
        return ResponseData.success(sysFeedbackMapper.reply(req.getId(),req.getReply(),loginInfoId));
    }

    @Override
    public ResponseData updateStatus(UpdateFeedBackStatusReq req,Long loginInfoId) {
        SysFeedback sysFeedback = sysFeedbackMapper.selectByPrimaryKey(req.getId());
        if (ObjectUtils.isEmpty(sysFeedback) ){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        if (2 == sysFeedback.getStatus()){
            return ResponseData.fail("此反馈已回复");
        }
        if (1 == sysFeedback.getStatus()){
            return ResponseData.fail("此反馈已读");
        }
        return ResponseData.success(sysFeedbackMapper.updateStatus(req.getId(),loginInfoId));
    }

    @Override
    public List<FeedbackPageResp> selectList(FeedbackPageReq req) {
        return sysFeedbackMapper.page(req);
    }

    @Override
    public ResponseData saveFeedback(SysFeedback sysFeedback) {
        return ResponseData.success(sysFeedbackMapper.insertSelective(sysFeedback));
    }

    @Override
    public ResponseData<PageInfo<HistoryFeedbackPageResp>> historyPage(FeedbackPageReq req, Long userInfoId) {
        PageHelper.startPage(req.getPageNo(), req.getPageSize());
        req.setUserInfoId(userInfoId);
        List<HistoryFeedbackPageResp> page = sysFeedbackMapper.historyPage(req);
        return ResponseData.success(new PageInfo<>(page));
    }

}
